import React from 'react';


import classnames from 'classnames';
//component
import './index.less';


export default class view extends React.Component {
    static propTypes = {
        max: React.PropTypes.number,
        min: React.PropTypes.number,

    };

    static defaultProps = {
        min: -Infinity,
        max: Infinity
    };

    constructor(props, context) {

        super(props, context);


        // init state;
        this.state = {

        };

    }
    componentWillMount() {
        this._isMounted = true;

    }

    componentDidMount() {

    }

    componentWillUnmount() {
        this._isMounted = false;
    }

    handleBlur(e, fun) {
        if (/(^\.$)|(^0+\.$)/.test(this.num) || this.num === '') {
            fun && fun(e, 0);
        }
    }

    handleChangeVal(e, fun) {
        let num = e.target.value;
        if (($$.utils.formRegular.number(num) || num === '')) {
            this.num = num;
            if (/^\.\d+$/.test(num)) num = +('0' + num);
            if (/^0+\d/.test(num)) num = +(num.replace(/^(0+)/, () => ''));
            if (num > this.props.max) num = this.props.max;
            if (num < this.props.min) num = this.props.min;
            fun && fun(e, num);
        }

    }

    render() {

        const classNames = classnames({
            'orin-sw-renumbertextfield-root': true,
            [this.props.className]: this.props.className ? true : false
        });

        var _props = {...this.props }
        _props.onChange && delete _props.onChange;
        _props.onBlur && delete _props.onBlur;
        _props.className && delete _props.className;
        _props.type && delete _props.type;
        _props.max && delete _props.max;
        _props.min && delete _props.min;

        return (
            <input
                {..._props} 
                className={classNames} 
                type="text"
                onChange = {(e) => this.handleChangeVal(e, this.props.onChange)}
                onBlur ={(e) =>{this.handleBlur(e, this.props.onChange); this.props.onBlur && this.props.onBlur();}}
            />
        );
    }
}
